home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Unix / satan-1.1.1 / perl / todo.pl < prev    next >
Text File  |  1995-04-10  |  4KB  |  176 lines

  1. #
  2. # add_todo($record) add one record to the new todo list.
  3. #
  4. # process_todos() iterates over the new todo list and generates new facts.
  5. #
  6. # save_todos($path) saves the old todos to the named file.
  7. #
  8. # drop_old_todos($host) forget everything we know about a host.
  9. #
  10. # read_todos($path) load tables from file.
  11. #
  12. # merge_todos($path) merge with in-core tables.
  13. #
  14. # version 2, Mon Mar 20 19:47:07 1995, last mod by wietse
  15. #
  16. require 'perl/shell.pl';
  17.  
  18. #
  19. # Add one probe to the new todo list.
  20. #
  21. sub add_todo {
  22. local($host, $tool, $args) = @_;
  23. local($record);
  24.  
  25. $record = "$host|$tool|$args";
  26.  
  27. if (!exists($old_todos{$record}) && !exists($new_todos{$record})) { 
  28.     $new_todos{$record} = $record; 
  29.     print "Add-todo: $record\n" if $debug;
  30.     }
  31. }
  32.  
  33. #
  34. # Add one probe to the new todo list, ignore args when looking for dups.
  35. #
  36. sub add_todo_ignore_args {
  37. local($host, $tool, $args) = @_;
  38. local($key, $record);
  39.  
  40. $key = "$host|$tool";
  41. $record = "$host|$tool|$args";
  42.  
  43. if (!exists($old_todos{$key})) { 
  44.     $new_todos{$key} = $record; 
  45.     print "Add-todo: $key ($args)\n" if $debug;
  46.     }
  47. }
  48.  
  49. #
  50. # Iterate over the new todo list until nothing new shows up. Skip tools
  51. # that we aren't supposed to run at this attack level.
  52. #
  53. sub process_todos {
  54. local($key,%temp_todos,$allowed_tools);
  55. local($target, $tool, $args, $level, $probe);
  56.  
  57. while (sizeof(*new_todos) > 0) {
  58.     %temp_todos = %new_todos;
  59.     %new_todos = ();
  60.     for $key (keys %temp_todos) {
  61.         ($target, $tool, $args) = split(/\|/, $temp_todos{$key}, 3);
  62.         next unless exists($all_hosts{$target});
  63.         $level = $all_hosts{$target}{'attack'};
  64.         next unless $level >= 0;
  65.         for $probe (@{$all_attacks[$level]}) {
  66.             if ($tool eq $probe || "$tool?" eq $probe || $probe eq "*?") {
  67.                 $old_todos{$key} = 1;
  68.                 &run_next_todo($target, $tool, $args);
  69.                 last;
  70.                 }
  71.             }
  72.         }
  73.     }
  74. }
  75.  
  76. #
  77. # Save old todo list to file.
  78. #
  79. sub save_todos {
  80. local($path) = @_;
  81.  
  82. open(TODO, ">$path") || die "cannot save old todo list to $path: $!";
  83. for $key (keys %old_todos) {
  84.     print TODO "$key\n";
  85.     }
  86. close(TODO);
  87. }
  88.  
  89. #
  90. # Reset todo tables and derivatives
  91. #
  92. sub clear_todos {
  93. %new_todos = ();
  94. %old_todos = ();
  95. }
  96.  
  97. #
  98. # Drop old entries on a specific host.
  99. #
  100. sub drop_old_todos {
  101.     local($host) = @_;
  102.     local($key, $target, $tool, $args);
  103.  
  104.     for $key (keys %old_todos) {
  105.         ($target, $tool, $args) = split(/\|/, $key);
  106.         delete $old_todos{$key} if $target eq $host;
  107.     }
  108. }
  109.  
  110. #
  111. # Read old todo list from file.
  112. #
  113. sub read_todos {
  114. local($path) = @_;
  115.  
  116. &clear_todos();
  117. &merge_todos($path);
  118. }
  119.  
  120. #
  121. # Merge old todo list with in-core table.
  122. #
  123. sub merge_todos {
  124. local($path) = @_;
  125.  
  126. open(TODO, $path) || die "cannot read old todo list from $path: $!";
  127. print "Reading old todo list from $path...\n" if $debug;
  128. while (<TODO>) {
  129.     chop;
  130.     $old_todos{$_} = 1;
  131.     }
  132. close(TODO);
  133. }
  134.  
  135. #
  136. # Run a tool and collect its output.
  137. #
  138. sub run_next_todo
  139. {
  140. local($target, $tool, $args) = @_;
  141. local($text, $ttl);
  142.  
  143. $ttl = (defined($timeouts{$tool}) ? $timeouts{$tool} : $timeout);
  144.  
  145. $command = "bin/$tool $args $target";
  146.  
  147. # Update host last access time.
  148. &set_host_time($target);
  149.  
  150. # Damn the torpedoes!
  151. die "Can't run $tool\n" unless &open_cmd(TOOL, $ttl, $command);
  152.  
  153. while (<TOOL>) {
  154.     chop;
  155.     &add_fact($_);
  156.     }
  157.  
  158. close(TOOL);
  159.  
  160. # Did we fly like the mighty turkey or soar like an...?
  161. # If the former, assume that we need to output an error record...
  162. if ($?) {
  163.     # based on exit value, decide what happened:
  164.     if ($? == $timeout_kill) { $text = "program timed out"; }
  165.     elsif ($? > 0 && $? < $timeout_kill) {
  166.         $text = "internal program error $?";
  167.         }
  168.     else { $text = "unknown error #$?"; }
  169.  
  170.     &add_fact("$target|$tool|u|||||$text");
  171.     }
  172.  
  173. }
  174.  
  175. 1;
  176.